const prefix = this.pageObj.getVariables().find(v => v.name === 'environmentType')?.value || '';
const playBack = this.getApiPage().getApiWidgetByAliasName('录像回放1')?.widgetObj;
const variables = this.getApiPage().getVariables()
function baseGet (url) {
    const headers = { "Content-type": "application/json" }
    return fetch(url, { headers }).then(res => res.json())
}

if ($event.type === 'DETAIL') {
    let eventData;
    try {
        eventData = JSON.parse($event.data)
    } catch (e) {
        console.error(e)
    }

    if ($event.typeName === '接警') {
        baseGet (`/${prefix}Event/event/receivingAlarm/${eventData.id}`)

        this.widgetObj.gisMap.removePopupInfo([eventData.id])
    }

    this.widgetObj.setIconPosition({ right: 420, top: 100 })
    this.widgetObj.setModalPosition({ right: 815, bottom: 20, maxWidth: 1510, width: 810, height: 286 })
    eventData.eventPoint = `${eventData.xCoordinate}，${eventData.yCoordinate}，${eventData.zCoordinate}`

    const { eventDetails } = eventData
    const detail = this.getApiPage().getApiWidgetsByClassName('detail')
    detail.forEach(v => v.showWidget())
    // 研判
    const details = this.getApiPage().getApiWidgetByAliasName('事件详情')?.widgetObj
    const $img = this.getApiPage().getApiWidgetByAliasName('detail-img')

    const formData = {
        ...eventData,
        eventDetails: JSON.stringify(eventDetails)
    }
    details.setData(formData)
    details.detail.render()
    const info = eventDetails
    if (Object.keys(info).length !== 0) {
        if (info.jpbPath) {
            // 打开回放
            const $playback = this.getApiPage().getApiWidgetByAliasName('playback')
            $playback.widgetObj.setData([{ data: info.jpbPath }])
            $playback.showWidget();
            $playback.widgetObj.render();
        } else {
            $playback.hideWidget();
            window.layer.msg('暂无录像回放')
        }
    } else {
        $img.widgetObj.setData([{ data: [] }])
        $img.widgetObj.render();

        $playback.hideWidget();
        window.layer.msg('暂无录像回放')
    }

    if (eventData.sourceDeviceId) {
        // 打开实时视频
        baseGet(`/${prefix}Basics/basics-main/system/equipmentObject/listDetailByEquipmentCode?equipmentCode=${eventData.sourceDeviceId}`)
            .then(json => {
                if (json?.success && json.result) {
                    const { rows } = json.result
                    const data = rows[0]?.equipmentConfig?.gbVideoConfig
                    var host = variables.find(x => x.name == "stream_url").value;
                    this.widgetObj.sullivan.close()
                    this.widgetObj.sullivan.initial()
                    if (data && data.sipUser && data.videoChannelId) {
                        this.widgetObj.sullivan.addPlayer(`${host}/api/v1/stream/rtc/play`, `${data.sipUser}/${data.videoChannelId}`)
                    } else {
                        window.layer.msg('暂无设备号/通道号')
                    }
                }
            })
    } else {
        this.widgetObj.sullivan.close();
        window.layer.msg('未关联摄像头');
    }

} else if ($event.type === 'sxjhf') {
    // 回放
    if ($event.data.equipmentConfig) {
        playBack.addRecorderEquipment({ equipmentConfig: $event.data.equipmentConfig });
    } else {
        window.layer?.msg('未找到录像机配置！');
    }
} else if ($event.type === 'DISPOSE') {
    var authTokenVar = variables.find(x => x.name == "AuthToken");
    window.parent.postMessage({ type: 'nav/style', pageName: '处置' }, document.referrer);
    let __data = $event.data;

    if (typeof __data !== 'string') {
        __data = JSON.stringify($event.data)
    }
    console.log('处置，路由。')
    this.getApiPage().navigateStage('833630884086153216', [
        { name: 'screen_params', value:  __data },
        { name: 'AuthToken', value: authTokenVar },
        { name: 'environmentType', value: prefix },
        { name: 'isOther', value: '1' },
    ])
}



